package cn.daomeng.firebird.core.mvc;

import cn.daomeng.firebird.core.exception.BizCodeEnumDefault;
import cn.daomeng.firebird.core.utils.StringUtils;

/**
 * @author qsl
 * @date 2019-04-15
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public class ApiResponse<T> {
	protected Integer code = Integer.MIN_VALUE;

	protected T data = null;

	protected String message = null;

	private ApiResponse() {
		this(null);
	}

	private ApiResponse(T data) {
		this(null, data, null);
	}
	
	private ApiResponse(T data, String message) {
		this(null, data, message);
	}
	
	private ApiResponse(Integer code, String message) {
		this(code, null, message);
	}
	
	private ApiResponse(Integer code, T data, String message) {
		if(null==code) {
			this.code = BizCodeEnumDefault.DEFAULT_SUCCESS.getValue();
		}
		else {
			this.code = code;
		}
		this.data = data;
		this.message = message;
	}

	public boolean isSuccess() {
		return this.getCode().equals(BizCodeEnumDefault.DEFAULT_SUCCESS.getValue());
	}

	
	
	public static boolean isSuccess(ApiResponse ApiResponse) {
		if (ApiResponse == null) {
			return false;
		}
		return ApiResponse.isSuccess();
	}

	/**
	 * 成功返回值
	 * 
	 * @return
	 */
	public static ApiResponse success() {
		return new ApiResponse();
	}

	/**
	 * 成功返回值
	 * 
	 * @param data 返回参数数据
	 * @return
	 */
	public static <T> ApiResponse success(T data) {
		return new ApiResponse(data);
	}

	/**
	 * 成功返回值
	 * 
	 * @param data
	 * @param message
	 * @param <T>
	 * @return
	 */
	public static <T> ApiResponse<T> success(T data, String message) {
		if (StringUtils.isEmpty(message)) {
			return new ApiResponse(data);
		} 
		return new ApiResponse(data, message);
	}

	/**
	 * 错误返回值
	 * 
	 * @param message 如果不传参数，或者位空字符串则使用，response_fail作为默认的返回值
	 * @return
	 */
	public static ApiResponse fail(String message) {
		if (StringUtils.isEmpty(message)) {
			message =  BizCodeEnumDefault.DEFAULT_FAILURE.getName();
		} 
		return new ApiResponse(BizCodeEnumDefault.DEFAULT_FAILURE.getValue(), message);
	}

	/**
	 * 错误返回值
	 * 
	 * @param message 如果不传参数，或者位空字符串则使用，response_fail作为默认的返回值
	 * @return
	 */
	public static <T> ApiResponse<T> fail(String message, T data) {
		if (StringUtils.isEmpty(message)) {
			message = BizCodeEnumDefault.DEFAULT_FAILURE.getName();
		} 
		return new ApiResponse(BizCodeEnumDefault.DEFAULT_FAILURE.getValue(), data, message);
	}

	public static ApiResponse fail(Throwable t) {
		return fail(t.getMessage());
	}

	/**
	 * 返回值
	 * 
	 * @param code
	 * @param message
	 * @return
	 */
	public static ApiResponse response(Integer code, String message) {
		return new ApiResponse(code, message);

	}

	/**
	 * 返回值
	 * 
	 * @param code code不能为空
	 * @param data
	 * @param <T>
	 * @return
	 */
	public static <T> ApiResponse<T> response(T data) {
		return new ApiResponse(data);
	}

	/**
	 * 返回值
	 * 
	 * @param code    code不传默认1000
	 * @param data
	 * @param message message为空默认response_success
	 * @param <T>
	 * @return
	 */
	public static <T> ApiResponse<T> response(Integer code, T data, String message) {
		return new ApiResponse(code, data, message);
	}

	
	
	
	//=========================  getter && setter ========================================================
	public Integer getCode() {
		return code;
	}

	public void setCode(Integer code) {
		this.code = code;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

}
